//
//  UIView+SLSDK.m
//  SLTool
//
//  Created by SongLian on 16/7/10.
//  Copyright © 2016年 SongLian. All rights reserved.
//

#import "UIView+SLSDK.h"
#import "SLConst.h"

@implementation UIView (SLSDK)

-(void)setSl_x:(CGFloat)sl_x {
    CGRect frame = self.frame;
    frame.origin.x = sl_x;
    self.frame = frame;
}

- (CGFloat)sl_x {
    return self.frame.origin.x;
}

- (void)setSl_y:(CGFloat)sl_y {
    CGRect frame = self.frame;
    frame.origin.y = sl_y;
    self.frame = frame;
}

- (CGFloat)sl_y {
    return self.frame.origin.y;
}

- (void)setSl_width:(CGFloat)sl_width {
    CGRect frame = self.frame;
    frame.size.width = sl_width;
    self.frame = frame;
}

- (CGFloat)sl_width {
    return self.frame.size.width;
}

- (void)setSl_height:(CGFloat)sl_height {
    CGRect frame = self.frame;
    frame.size.height = sl_height;
    self.frame = frame;
}

- (CGFloat)sl_height {
    return self.frame.size.height;
}

- (void)setSl_size:(CGSize)sl_size {
    CGRect frame = self.frame;
    frame.size = sl_size;
    self.frame = frame;
}

- (CGSize)sl_size {
    return self.frame.size;
}

- (void)setSl_centerX:(CGFloat)sl_centerX {
    CGPoint center = self.center;
    center.x = sl_centerX;
    self.center = center;
}

- (CGFloat)sl_centerX {
    return self.center.x;
}

- (void)setSl_centerY:(CGFloat)sl_centerY {
    CGPoint center = self.center;
    center.y = sl_centerY;
    self.center = center;
}

- (CGFloat)sl_centerY {
    return self.center.y;
}


- (void)setSl_origin:(CGPoint)sl_origin {
    CGRect frame = self.frame;
    frame.origin = sl_origin;
    self.frame = frame;
}

- (CGPoint)sl_origin {
    return self.frame.origin;
}

- (CGFloat)sl_halfWidth {
    return self.sl_width * SLFloat_Half;
}

- (CGFloat)sl_halfHeight {
    return self.sl_height * SLFloat_Half;
}

- (CGFloat)sl_maxX {
    return CGRectGetMaxX(self.frame);
}

- (void)setSl_maxX:(CGFloat)sl_maxX {
    self.sl_width += sl_maxX - self.sl_maxX;
}

- (CGFloat)sl_maxY {
    return CGRectGetMaxY(self.frame);
}

- (void)setSl_maxY:(CGFloat)sl_maxY {
    self.sl_height += sl_maxY - self.sl_maxY;
}

- (UIViewController *)sl_viewController {
    for (UIView *view = self; view; view = view.superview) {
        UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]])
            return (UIViewController *)nextResponder;
    }
    return nil;
}

#pragma mark - --- Fillet ---
- (void)sl_filletWithRadius:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}

- (void)sl_filletWithHalfHeight {
    [self sl_filletWithRadius:self.sl_halfHeight];
}

- (void)sl_filletWithHalfWidth {
    [self sl_filletWithRadius:self.sl_halfWidth];
}

- (void)sl_addSubviews:(NSArray *)subviews {
    for (UIView *view in subviews) {
        [self addSubview:view];
    }
}

+ (UIView *)sl_viewWithFrame:(CGRect)frame backgroudColor:(UIColor *)backgroundColor {
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = backgroundColor;
    return view;
}

+ (UIView *)sl_viewWithNibName:(NSString *)nibName {
    UIView *view = [[[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil] firstObject];
    return view;
}

+ (UIView *)sl_starsViewWithRanking:(CGFloat)ranking origin:(CGPoint)origin {
    
    UIView *backgroudView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 90, 12)];
    backgroudView.sl_origin = origin;
    
    NSInteger yellowNumber = ranking;
    NSInteger grayNumber = 5 - ranking;
    NSInteger halfNumber = 5 - yellowNumber - grayNumber;
    
    NSString *yellowStar = @"arround_stars_allYellow";
    NSString *halfStar = @"arround_stars_TwoPart";
    NSString *grayStar = @"arround_stars_allgray";
    
    for (NSInteger i = 0; i < 5; i++) {
        
        NSString *starString = i < yellowNumber ? yellowStar : (i < halfNumber+yellowNumber ? halfStar : grayStar);
        UIImageView *starImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:starString]];
        starImageView.sl_origin = CGPointMake(12*i, 0);
        
        [backgroudView addSubview:starImageView];
    }
    
//    UILabel *rankingLabel = [SLTool createLabelWithTitle:[NSString stringWithFormat:@"%.1f", ranking] frame:CGRectMake(60, 0, 30, 12) textColor:RGBColor(248, 102, 32) titleSize:14 textAlignment:NSTextAlignmentCenter];
//    [backgroudView addSubview:rankingLabel];
    
    return backgroudView;
}

- (void)sl_layoutWithView:(UIView *)view {
    NSLayoutConstraint *layoutConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:10];
    [self addConstraint:layoutConstraint];
}


@end
